//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.jpmml.evaluator;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.dmg.pmml.FieldName;
import org.dmg.pmml.MiningSchema;
import org.dmg.pmml.Model;
import org.dmg.pmml.PMML;

/**
 * @Author shen
 * @Description 覆盖原检查逻辑
 * @Date 14:24 2022/6/21
 * @Param
 * @return
 **/
public class ModelEvaluatorBuilder implements EvaluatorBuilder, Serializable {
    private PMML pmml;
    private Model model;
    private ConfigurationBuilder configurationBuilder;
    private InputMapper inputMapper;
    private ResultMapper resultMapper;

    protected ModelEvaluatorBuilder() {
        this.pmml = null;
        this.model = null;
        this.configurationBuilder = new ConfigurationBuilder();
        this.inputMapper = null;
        this.resultMapper = null;
    }

    public ModelEvaluatorBuilder(PMML pmml) {
        this(pmml, (String)null);
    }

    public ModelEvaluatorBuilder(PMML pmml, String modelName) {
        this.pmml = null;
        this.model = null;
        this.configurationBuilder = new ConfigurationBuilder();
        this.inputMapper = null;
        this.resultMapper = null;
        this.setPMML((PMML)Objects.requireNonNull(pmml));
        this.setModel(PMMLUtil.findModel(pmml, modelName));
    }

    public ModelEvaluatorBuilder(PMML pmml, Model model) {
        this.pmml = null;
        this.model = null;
        this.configurationBuilder = new ConfigurationBuilder();
        this.inputMapper = null;
        this.resultMapper = null;
        this.setPMML((PMML)Objects.requireNonNull(pmml));
        this.setModel((Model)Objects.requireNonNull(model));
    }

    public ModelEvaluatorBuilder clone() {
        ModelEvaluatorBuilder modelEvaluatorBuilder;
        try {
            modelEvaluatorBuilder = (ModelEvaluatorBuilder)super.clone();
        } catch (CloneNotSupportedException var3) {
            throw new InternalError(var3);
        }

        modelEvaluatorBuilder.configurationBuilder = modelEvaluatorBuilder.configurationBuilder.clone();
        return modelEvaluatorBuilder;
    }

    public ModelEvaluator<?> build() {
        PMML pmml = this.getPMML();
        Model model = this.getModel();
        if (pmml != null && model != null) {
            ConfigurationBuilder configurationBuilder = this.getConfigurationBuilder();
            Configuration configuration = configurationBuilder.build();
            ModelEvaluatorFactory modelEvaluatorFactory = configuration.getModelEvaluatorFactory();
            ModelEvaluator<?> modelEvaluator = modelEvaluatorFactory.newModelEvaluator(pmml, model);
            modelEvaluator.configure(configuration);
            InputMapper inputMapper = this.getInputMapper();
            ResultMapper resultMapper = this.getResultMapper();
            modelEvaluator.setInputMapper(inputMapper);
            modelEvaluator.setResultMapper(resultMapper);
            this.checkSchema(modelEvaluator);
            return modelEvaluator;
        } else {
            throw new IllegalStateException();
        }
    }

    protected void checkSchema(ModelEvaluator<?> modelEvaluator) {
        Model model = modelEvaluator.getModel();
        MiningSchema miningSchema = model.getMiningSchema();
        List<InputField> inputFields = modelEvaluator.getInputFields();
        List<InputField> groupFields = Collections.emptyList();
        if (modelEvaluator instanceof HasGroupFields) {
            HasGroupFields hasGroupFields = (HasGroupFields)modelEvaluator;
            groupFields = hasGroupFields.getGroupFields();
        }

//        if (inputFields.size() + groupFields.size() > 1000) {
//            throw new InvalidElementException("Model has too many input fields", miningSchema);
//        } else {
        // 我们不关注长度
        List<TargetField> targetFields = modelEvaluator.getTargetFields();
        List<OutputField> outputFields = modelEvaluator.getOutputFields();
        if (targetFields.size() + outputFields.size() < 1) {
            throw new InvalidElementException("Model does not have any target or output fields", miningSchema);
        }
//        }
    }

    public PMML getPMML() {
        return this.pmml;
    }

    protected ModelEvaluatorBuilder setPMML(PMML pmml) {
        this.pmml = pmml;
        return this;
    }

    public Model getModel() {
        return this.model;
    }

    protected ModelEvaluatorBuilder setModel(Model model) {
        this.model = model;
        return this;
    }

    public ConfigurationBuilder getConfigurationBuilder() {
        return this.configurationBuilder;
    }

    public ModelEvaluatorFactory getModelEvaluatorFactory() {
        ConfigurationBuilder configurationBuilder = this.getConfigurationBuilder();
        return configurationBuilder.getModelEvaluatorFactory();
    }

    public ModelEvaluatorBuilder setModelEvaluatorFactory(ModelEvaluatorFactory modelEvaluatorFactory) {
        ConfigurationBuilder configurationBuilder = this.getConfigurationBuilder();
        configurationBuilder.setModelEvaluatorFactory(modelEvaluatorFactory);
        return this;
    }

    public ValueFactoryFactory getValueFactoryFactory() {
        ConfigurationBuilder configurationBuilder = this.getConfigurationBuilder();
        return configurationBuilder.getValueFactoryFactory();
    }

    public ModelEvaluatorBuilder setValueFactoryFactory(ValueFactoryFactory valueFactoryFactory) {
        ConfigurationBuilder configurationBuilder = this.getConfigurationBuilder();
        configurationBuilder.setValueFactoryFactory(valueFactoryFactory);
        return this;
    }

    public OutputFilter getOutputFilter() {
        ConfigurationBuilder configurationBuilder = this.getConfigurationBuilder();
        return configurationBuilder.getOutputFilter();
    }

    public ModelEvaluatorBuilder setOutputFilter(OutputFilter outputFilter) {
        ConfigurationBuilder configurationBuilder = this.getConfigurationBuilder();
        configurationBuilder.setOutputFilter(outputFilter);
        return this;
    }

    public SymbolTable<FieldName> getDerivedFieldGuard() {
        ConfigurationBuilder configurationBuilder = this.getConfigurationBuilder();
        return configurationBuilder.getDerivedFieldGuard();
    }

    public ModelEvaluatorBuilder setDerivedFieldGuard(SymbolTable<FieldName> derivedFieldGuard) {
        ConfigurationBuilder configurationBuilder = this.getConfigurationBuilder();
        configurationBuilder.setDerivedFieldGuard(derivedFieldGuard);
        return this;
    }

    public SymbolTable<String> getFunctionGuard() {
        ConfigurationBuilder configurationBuilder = this.getConfigurationBuilder();
        return configurationBuilder.getFunctionGuard();
    }

    public ModelEvaluatorBuilder setFunctionGuard(SymbolTable<String> functionGuard) {
        ConfigurationBuilder configurationBuilder = this.getConfigurationBuilder();
        configurationBuilder.setFunctionGuard(functionGuard);
        return this;
    }

    public InputMapper getInputMapper() {
        return this.inputMapper;
    }

    public ModelEvaluatorBuilder setInputMapper(InputMapper inputMapper) {
        this.inputMapper = inputMapper;
        return this;
    }

    public ResultMapper getResultMapper() {
        return this.resultMapper;
    }

    public ModelEvaluatorBuilder setResultMapper(ResultMapper resultMapper) {
        this.resultMapper = resultMapper;
        return this;
    }
}
